import sys
import os

# 添加项目根目录和backend目录到Python路径
current_dir = os.path.dirname(os.path.abspath(__file__))
project_dir = os.path.dirname(os.path.dirname(current_dir))
backend_dir = os.path.join(project_dir, "backend")
sys.path.extend([project_dir, backend_dir])

# 从backend目录导入database模块
sys.path.append(backend_dir)
from database import database

def save_fault_data_to_database(counters, counters1):
    """将故障统计数据保存到数据库"""
    try:
        # 初始化数据库
        database.init_database()
        
        # 先清空现有数据并重置自增ID，避免重复
        conn = database.get_db_connection()
        cursor = conn.cursor()
        cursor.execute("DELETE FROM fault_counts")
        cursor.execute("UPDATE sqlite_sequence SET seq = 0 WHERE name = 'fault_counts'")
        cursor.execute("DELETE FROM manufacturer_stats")
        cursor.execute("UPDATE sqlite_sequence SET seq = 0 WHERE name = 'manufacturer_stats'")
        conn.commit()
        conn.close()
        
        # 保存故障数量统计
        # 一期
        # 移除了 > 0 的条件判断，确保即使故障处理次数为0也插入记录
       
        database.insert_fault_counts(('一期', 'dmz', '华为', counters.get('d52', 0)))
        database.insert_fault_counts(('一期', '可信1', '华为', counters.get('d53', 0)))
        database.insert_fault_counts(('一期', '可信2', '中兴', counters.get('d54', 0)))
            
        # 二期
        
        database.insert_fault_counts(('二期', 'dmz', '华为', counters.get('d55', 0)))
        database.insert_fault_counts(('二期', '可信1', '华为', counters.get('d56', 0)))
        database.insert_fault_counts(('二期', '可信2', '中兴', counters.get('d57', 0)))
        database.insert_fault_counts(('二期', '可信3', '中兴', counters.get('d58', 0)))
        database.insert_fault_counts(('二期', '可信4', '华为', counters.get('d59', 0)))
        database.insert_fault_counts(('二期', '可信5', '华为', counters.get('d60', 0)))
        database.insert_fault_counts(('二期', '网管6', '华为', counters.get('d61', 0)))
            
        # 三期
        
        database.insert_fault_counts(('三期', 'dmz', '华为', counters.get('d62', 0)))
        database.insert_fault_counts(('三期', '可信2', '中兴', counters.get('d63', 0)))
        database.insert_fault_counts(('三期', '可信4', '华为', counters.get('d64', 0)))
        database.insert_fault_counts(('三期', '可信5', '华为', counters.get('d65', 0)))
        database.insert_fault_counts(('三期', '网管6', '华为', counters.get('d66', 0)))
        database.insert_fault_counts(('三期', '可信7', '中兴', counters.get('d67', 0)))
            
        # 四期
        
        database.insert_fault_counts(('四期', '可信8', '中兴', counters.get('d68', 0)))
        database.insert_fault_counts(('四期', '网管6', '华为', counters.get('d69', 0)))
        
        # 保存厂家故障统计（详细部件统计）
        # 构建厂家统计数据
        manufacturer_stats = []
        
        # 新华三存储
        h3c_storage_count = sum([
            counters1.get('d70', 0), counters1.get('d71', 0), counters1.get('d72', 0),
            counters1.get('d73', 0), counters1.get('d74', 0), counters1.get('d75', 0),
            counters1.get('d76', 0), counters1.get('d77', 0), counters1.get('d78', 0),
            counters1.get('d79', 0), counters1.get('d80', 0), counters1.get('d81', 0),
            counters1.get('d82', 0), counters1.get('d83', 0), counters1.get('d84', 0),
            counters1.get('d85', 0)
        ])
        # 移除了 > 0 的条件判断，确保即使故障处理次数为0也插入记录
        stats_data = (
            '新华三', '存储',
            counters1.get('d76', 0),  # optical_module
            counters1.get('d71', 0),  # cpu_non_domestic
            0,  # cpu_domestic_hygon
            0,  # cpu_domestic_arm
            counters1.get('d77', 0),  # memory
            counters1.get('d70', 0),  # disk_non_domestic
            0,  # disk_domestic
            counters1.get('d78', 0),  # cache_disk
            counters1.get('d79', 0),  # backplane
            counters1.get('d72', 0),  # nic_non_domestic
            0,  # nic_domestic
            counters1.get('d80', 0),  # sensor
            counters1.get('d81', 0),  # riser_card
            counters1.get('d73', 0),  # pcie_card_non_domestic
            0,  # pcie_card_domestic
            0,  # motherboard
            0,  # power_supply
            counters1.get('d74', 0),  # raid_card_non_domestic
            0,  # raid_card_domestic
            counters1.get('d84', 0),  # fiber
            counters1.get('d75', 0),  # fan_non_domestic
            0,  # fan_domestic
            counters1.get('d85', 0),  # whole_machine
            h3c_storage_count  # total_count
        )
        database.insert_manufacturer_stats(stats_data)
        
        # 超聚变计算
        xfusion_computer_count = sum([
            counters1.get('d86', 0), counters1.get('d87', 0), counters1.get('d88', 0),
            counters1.get('d89', 0), counters1.get('d90', 0), counters1.get('d91', 0),
            counters1.get('d92', 0), counters1.get('d93', 0), counters1.get('d94', 0),
            counters1.get('d95', 0), counters1.get('d96', 0), counters1.get('d97', 0),
            counters1.get('d98', 0), counters1.get('d99', 0), counters1.get('d100', 0), 
            counters1.get('d101', 0)
        ])
        # 移除了 > 0 的条件判断，确保即使故障处理次数为0也插入记录
        # 重新计算包含主板和电源的统计
        motherboard_count = counters1.get('d98', 0)
        power_supply_count = counters1.get('d99', 0)
        
        stats_data = (
            '超聚变', '计算',
            counters1.get('d92', 0),  # optical_module
            counters1.get('d87', 0),  # cpu_non_domestic
            0,  # cpu_domestic_hygon
            0,  # cpu_domestic_arm
            counters1.get('d93', 0),  # memory
            counters1.get('d86', 0),  # disk_non_domestic
            0,  # disk_domestic
            counters1.get('d94', 0),  # cache_disk
            counters1.get('d95', 0),  # backplane
            counters1.get('d88', 0),  # nic_non_domestic
            0,  # nic_domestic
            counters1.get('d96', 0),  # sensor
            counters1.get('d97', 0),  # riser_card
            counters1.get('d91', 0),  # pcie_card_non_domestic
            0,  # pcie_card_domestic
            motherboard_count,  # motherboard
            power_supply_count,  # power_supply
            counters1.get('d89', 0),  # raid_card_non_domestic
            0,  # raid_card_domestic
            counters1.get('d100', 0),  # fiber
            counters1.get('d90', 0),  # fan_non_domestic
            0,  # fan_domestic
            counters1.get('d101', 0),  # whole_machine
            xfusion_computer_count  # total_count
        )
        database.insert_manufacturer_stats(stats_data)
        
        # 华为存储
        huawei_storage_count = sum([
            counters1.get('d102', 0), counters1.get('d103', 0), counters1.get('d104', 0),
            counters1.get('d105', 0), counters1.get('d106', 0), counters1.get('d107', 0),
            counters1.get('d108', 0), counters1.get('d109', 0), counters1.get('d110', 0),
            counters1.get('d111', 0), counters1.get('d112', 0), counters1.get('d113', 0),
            counters1.get('d114', 0), counters1.get('d115', 0), counters1.get('d116', 0),
            counters1.get('d117', 0)
        ])
        # 移除了 > 0 的条件判断，确保即使故障处理次数为0也插入记录
        stats_data = (
            '华为', '存储',
            counters1.get('d108', 0),  # optical_module
            counters1.get('d103', 0),  # cpu_non_domestic
            0,  # cpu_domestic_hygon
            0,  # cpu_domestic_arm
            counters1.get('d109', 0),  # memory
            counters1.get('d102', 0),  # disk_non_domestic
            0,  # disk_domestic
            counters1.get('d110', 0),  # cache_disk
            counters1.get('d111', 0),  # backplane
            counters1.get('d104', 0),  # nic_non_domestic
            0,  # nic_domestic
            counters1.get('d112', 0),  # sensor
            counters1.get('d113', 0),  # riser_card
            counters1.get('d105', 0),  # pcie_card_non_domestic
            0,  # pcie_card_domestic
            0,  # motherboard
            0,  # power_supply
            counters1.get('d106', 0),  # raid_card_non_domestic
            0,  # raid_card_domestic
            counters1.get('d116', 0),  # fiber
            counters1.get('d107', 0),  # fan_non_domestic
            0,  # fan_domestic
            counters1.get('d117', 0),  # whole_machine
            huawei_storage_count  # total_count
        )
        database.insert_manufacturer_stats(stats_data)
        
        # 中兴计算
        zte_computer_count = sum([
            counters1.get('d118', 0), counters1.get('d119', 0), counters1.get('d120', 0),
            counters1.get('d121', 0), counters1.get('d122', 0), counters1.get('d123', 0),
            counters1.get('d124', 0), counters1.get('d125', 0), counters1.get('d126', 0),
            counters1.get('d127', 0), counters1.get('d128', 0), counters1.get('d129', 0),
            counters1.get('d130', 0), counters1.get('d131', 0), counters1.get('d132', 0),
            counters1.get('d133', 0), counters1.get('d134', 0), counters1.get('d135', 0)
        ])
        # 移除了 > 0 的条件判断，确保即使故障处理次数为0也插入记录
        stats_data = (
            '中兴', '计算',
            counters1.get('d126', 0),  # optical_module
            counters1.get('d119', 0),  # cpu_non_domestic
            0,  # cpu_domestic_hygon (需要根据是否国产判断)
            0,  # cpu_domestic_arm
            counters1.get('d127', 0),  # memory
            counters1.get('d118', 0),  # disk_non_domestic
            counters1.get('d123', 0),  # disk_domestic
            counters1.get('d128', 0),  # cache_disk
            counters1.get('d129', 0),  # backplane
            counters1.get('d120', 0),  # nic_non_domestic
            0,  # nic_domestic
            counters1.get('d130', 0),  # sensor
            counters1.get('d131', 0),  # riser_card
            counters1.get('d121', 0),  # pcie_card_non_domestic
            0,  # pcie_card_domestic
            0,  # motherboard
            0,  # power_supply
            counters1.get('d122', 0),  # raid_card_non_domestic
            0,  # raid_card_domestic
            counters1.get('d134', 0),  # fiber
            counters1.get('d125', 0),  # fan_non_domestic
            0,  # fan_domestic
            counters1.get('d135', 0),  # whole_machine
            zte_computer_count  # total_count
        )
        database.insert_manufacturer_stats(stats_data)
        
        # 中兴存储
        zte_storage_count = sum([
            counters1.get('d136', 0), counters1.get('d137', 0), counters1.get('d138', 0),
            counters1.get('d139', 0), counters1.get('d140', 0), counters1.get('d141', 0),
            counters1.get('d142', 0), counters1.get('d143', 0), counters1.get('d144', 0),
            counters1.get('d145', 0), counters1.get('d146', 0), counters1.get('d147', 0),
            counters1.get('d148', 0), counters1.get('d149', 0), counters1.get('d150', 0),
            counters1.get('d151', 0), counters1.get('d152', 0), counters1.get('d153', 0)
        ])
        # 移除了 > 0 的条件判断，确保即使故障处理次数为0也插入记录
        stats_data = (
            '中兴', '存储',
            counters1.get('d144', 0),  # optical_module
            counters1.get('d137', 0),  # cpu_non_domestic
            0,  # cpu_domestic_hygon
            0,  # cpu_domestic_arm
            counters1.get('d145', 0),  # memory
            counters1.get('d136', 0),  # disk_non_domestic
            counters1.get('d142', 0),  # disk_domestic
            counters1.get('d146', 0),  # cache_disk
            counters1.get('d147', 0),  # backplane
            counters1.get('d138', 0),  # nic_non_domestic
            0,  # nic_domestic
            counters1.get('d148', 0),  # sensor
            counters1.get('d149', 0),  # riser_card
            counters1.get('d139', 0),  # pcie_card_non_domestic
            0,  # pcie_card_domestic
            0,  # motherboard
            0,  # power_supply
            counters1.get('d140', 0),  # raid_card_non_domestic
            0,  # raid_card_domestic
            counters1.get('d152', 0),  # fiber
            counters1.get('d141', 0),  # fan_non_domestic
            0,  # fan_domestic
            counters1.get('d153', 0),  # whole_machine
            zte_storage_count  # total_count
        )
        database.insert_manufacturer_stats(stats_data)
        
        # 浪潮计算
        inspur_computer_count = sum([
            counters1.get('d154', 0), counters1.get('d155', 0), counters1.get('d156', 0),
            counters1.get('d157', 0), counters1.get('d158', 0), counters1.get('d159', 0),
            counters1.get('d160', 0), counters1.get('d161', 0), counters1.get('d162', 0),
            counters1.get('d163', 0), counters1.get('d164', 0), counters1.get('d165', 0),
            counters1.get('d166', 0), counters1.get('d167', 0), counters1.get('d168', 0),
            counters1.get('d169', 0), counters1.get('d170', 0), counters1.get('d171', 0)
        ])
        # 移除了 > 0 的条件判断，确保即使故障处理次数为0也插入记录
        stats_data = (
            '浪潮', '计算',
            counters1.get('d162', 0),  # optical_module
            counters1.get('d155', 0),  # cpu_non_domestic
            counters1.get('d157', 0),  # cpu_domestic_hygon
            0,  # cpu_domestic_arm
            counters1.get('d163', 0),  # memory
            counters1.get('d154', 0),  # disk_non_domestic
            0,  # disk_domestic
            counters1.get('d164', 0),  # cache_disk
            counters1.get('d165', 0),  # backplane
            counters1.get('d158', 0),  # nic_non_domestic
            counters1.get('d159', 0),  # nic_domestic
            counters1.get('d166', 0),  # sensor
            counters1.get('d167', 0),  # riser_card
            counters1.get('d156', 0),  # pcie_card_non_domestic
            counters1.get('d159', 0),  # pcie_card_domestic
            0,  # motherboard
            0,  # power_supply
            counters1.get('d160', 0),  # raid_card_non_domestic
            0,  # raid_card_domestic
            counters1.get('d170', 0),  # fiber
            counters1.get('d161', 0),  # fan_non_domestic
            counters1.get('d161', 0),  # fan_domestic
            counters1.get('d171', 0),  # whole_machine
            inspur_computer_count  # total_count
        )
        database.insert_manufacturer_stats(stats_data)
        
        # 浪潮存储
        inspur_storage_count = sum([
            counters1.get('d172', 0), counters1.get('d173', 0), counters1.get('d174', 0),
            counters1.get('d175', 0), counters1.get('d176', 0), counters1.get('d177', 0),
            counters1.get('d178', 0), counters1.get('d179', 0), counters1.get('d180', 0),
            counters1.get('d181', 0), counters1.get('d182', 0), counters1.get('d183', 0),
            counters1.get('d184', 0), counters1.get('d185', 0), counters1.get('d186', 0),
            counters1.get('d187', 0), counters1.get('d188', 0), counters1.get('d189', 0),
            counters1.get('d190', 0)
        ])
        # 移除了 > 0 的条件判断，确保即使故障处理次数为0也插入记录
        stats_data = (
            '浪潮', '存储',
            counters1.get('d180', 0),  # optical_module
            counters1.get('d173', 0),  # cpu_non_domestic
            counters1.get('d175', 0),  # cpu_domestic_hygon
            0,  # cpu_domestic_arm
            counters1.get('d181', 0),  # memory
            counters1.get('d172', 0),  # disk_non_domestic
            counters1.get('d190', 0),  # disk_domestic
            counters1.get('d182', 0),  # cache_disk
            counters1.get('d183', 0),  # backplane
            counters1.get('d176', 0),  # nic_non_domestic
            counters1.get('d177', 0),  # nic_domestic
            counters1.get('d184', 0),  # sensor
            counters1.get('d185', 0),  # riser_card
            counters1.get('d174', 0),  # pcie_card_non_domestic
            counters1.get('d177', 0),  # pcie_card_domestic
            0,  # motherboard
            0,  # power_supply
            counters1.get('d178', 0),  # raid_card_non_domestic
            0,  # raid_card_domestic
            counters1.get('d188', 0),  # fiber
            counters1.get('d179', 0),  # fan_non_domestic
            counters1.get('d179', 0),  # fan_domestic
            counters1.get('d189', 0),  # whole_machine
            inspur_storage_count  # total_count
        )
        database.insert_manufacturer_stats(stats_data)
            
        print("故障统计数据已成功保存到数据库")
        return True
    except Exception as e:
        print(f"保存故障统计数据到数据库时出错: {str(e)}")
        return False

def save_fault_details_to_database(fault_details):
    """将故障明细数据保存到数据库"""
    try:
        # 初始化数据库
        database.init_database()
        
        # 先清空现有数据并重置自增ID，避免重复
        conn = database.get_db_connection()
        cursor = conn.cursor()
        cursor.execute("DELETE FROM fault_details")
        cursor.execute("UPDATE sqlite_sequence SET seq = 0 WHERE name = 'fault_details'")
        conn.commit()
        conn.close()
        
        # 保存故障明细数据
        for detail in fault_details:
            database.insert_fault_details(detail)
            
        print(f"故障明细数据已成功保存到数据库，共保存了 {len(fault_details)} 条记录")
        return True
    except Exception as e:
        print(f"保存故障明细数据到数据库时出错: {str(e)}")
        return False

def save_fault_description_to_database(description):
    """将详细故障描述保存到数据库"""
    try:
        # 初始化数据库
        database.init_database()
        
        # 先清空现有数据，避免重复
        conn = database.get_db_connection()
        cursor = conn.cursor()
        cursor.execute("DELETE FROM fault_descriptions")
        # 注意：fault_descriptions表没有自增ID，所以不需要重置序列
        conn.commit()
        conn.close()
        
        # 保存详细故障描述
        database.insert_fault_description(description)
        
        print("详细故障描述已成功保存到数据库")
        return True
    except Exception as e:
        print(f"保存详细故障描述到数据库时出错: {str(e)}")
        return False